package com.eastfair.messagecenter.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.messagecenter.dto.*;
import com.eastfair.messagecenter.entity.InnerTemplate;
import com.eastfair.messagecenter.entity.SmsTemplate;
import com.eastfair.messagecenter.exceptioncode.MessageServiceExceptionCode;
import com.eastfair.messagecenter.service.SmsTemplateService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.messagecenter.vo.SmsTemplateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 短信模板
 * </p>
 *
 * @author ligang
 * @date 2022-09-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/smsTemplate")
@Api(value = "SmsTemplate", tags = "短信模板")
@PreAuth(replace = "messagecenter:smsTemplate:")
public class SmsTemplateController extends SuperController<SmsTemplateService, Long, SmsTemplate, SmsTemplatePageQuery, SmsTemplateSaveDTO, SmsTemplateUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<SmsTemplate> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<SmsTemplate> smsTemplateList = list.stream().map((map) -> {
            SmsTemplate smsTemplate = SmsTemplate.builder().build();
            //TODO 请在这里完成转换
            return smsTemplate;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(smsTemplateList));
    }


    /**
     * 自定义新增
     *
     * @param model 保存对象
     * @return 返回SUCCESS_RESPONSE, 调用默认更新, 返回其他不调用默认更新
     */
    @Override
    public R<SmsTemplate> handlerSave(SmsTemplateSaveDTO model) {
        SmsTemplateDTO smsTemplateDTO = new SmsTemplateDTO();
        smsTemplateDTO.setTemplateCode(model.getTemplateCode());
        smsTemplateDTO.setSystemScene(model.getSystemScene());
        if ("OTHER".equals(model.getTemplateCode()) || "OTHERS".equals(model.getTemplateCode())) {
            return R.successDef();
        }
        List<SmsTemplate> smsTemplateList = baseService.list(smsTemplateDTO, null);
        if (CollectionUtil.isNotEmpty(smsTemplateList)) {
            return R.fail(MessageServiceExceptionCode.TEMPLATE_AGAIN);
        }
        return R.successDef();
    }

    /**
     * 自定义更新
     *
     * @param model 修改DTO
     * @return 返回SUCCESS_RESPONSE, 调用默认更新, 返回其他不调用默认更新
     */
    @Override
    public R<SmsTemplate> handlerUpdate(SmsTemplateUpdateDTO model) {
        SmsTemplateDTO smsTemplateDTO = new SmsTemplateDTO();
        smsTemplateDTO.setTemplateCode(model.getTemplateCode());
        smsTemplateDTO.setSystemScene(model.getSystemScene());
        if ("OTHER".equals(model.getTemplateCode()) || "OTHERS".equals(model.getTemplateCode())) {
            return R.successDef();
        }
        List<SmsTemplate> smsTemplateList = baseService.list(smsTemplateDTO, null);
        if (CollectionUtil.isNotEmpty(smsTemplateList)) {
            return R.fail(MessageServiceExceptionCode.TEMPLATE_AGAIN);
        }

        return R.successDef();
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<SmsTemplatePageQuery> params) {
        SmsTemplatePageQuery pageQuery = params.getModel();
//        if(StringUtils.isBlank(pageQuery.getSystemScene())){
//            pageQuery.setSystemScene(ContextUtil.getSystemScene());
//        }
    }


    /**
     * 手机短信模板详情
     *
     * @param id
     * @return 是否成功
     */
    @ApiOperation(value = "手机短信模板详情", notes = "手机短信模板详情")
    @GetMapping("/detail")
    public R<SmsTemplateVO> detail(Long id) {
        //校验动态表单
        SmsTemplate smsTemplate = baseService.getByIdCache(id);
        return R.success(baseService.doToVo(smsTemplate));
    }

    /**
     * 查询手机模板
     *
     * @param smsTemplateDTO
     * @return 是否成功
     */
    @ApiOperation(value = "查询手机模板", notes = "查询手机模板")
    @PostMapping("/listVO")
    public R<List<SmsTemplateVO>> listVO(@RequestBody SmsTemplateDTO smsTemplateDTO) {
        //校验动态表单
        List<SmsTemplateVO> smsTemplateVOS = baseService.doToVos(baseService.list(smsTemplateDTO, null));
        return R.success(smsTemplateVOS);
    }
}
